//
//  AddressBookLeftViewController.m
//  SouthChina
//
//  Created by Mr.Y on 14/11/25.
//  Copyright (c) 2014年 科创. All rights reserved.
//

#import "AddressBookLeftViewController.h"
#import "TabbarViewController.h"
#import "ContactDao.h"
#import "AddressBookLeftTableViewCell.h"
#import "AddressBookViewController.h"
@interface AddressBookLeftViewController ()<UITableViewDelegate,UITableViewDataSource>
{
    NSArray * orglist;
    NSString *selectOrgID;
}
@property(nonatomic, strong) NSArray *titles;
@property(nonatomic, strong) UITableView * groupTableview;
@property(strong, nonatomic) UISwitch *activateSwitchBtn;

@end

@implementation AddressBookLeftViewController

@synthesize groupdelegate;
@synthesize isCreatGroup;


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
  
    }
    
    return self;
}
- (id)initWithTitles:(NSArray *)titles
{
    NSParameterAssert(titles);
    
    if (self) {
        _titles = titles;
    }
    return self;
}
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:YES];
    
}
#pragma mark - Managing the view

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    if (!isCreatGroup) {
        // 创建群组按钮
        UIButton * addGroupBtn = [[UIButton alloc] initWithFrame:CGRectMake(0,(YNEW), (SCREEN_WIDTH)-(SCREEN_WIDTH)*0.2, 44)];
        [addGroupBtn addTarget:self action:@selector(addGroupBtn:) forControlEvents:UIControlEventTouchUpInside];
        [addGroupBtn setTitle:@"" forState:UIControlStateNormal];
        [addGroupBtn setBackgroundColor:[UIColor colorWithRed:236/255.0f green:235/255.0f blue:243/255.0f alpha:1]];
        [self.view addSubview:addGroupBtn];
        
        UILabel * addGroupLabel = [[UILabel alloc] initWithFrame:CGRectMake(100,5, 100, 30)];
        addGroupLabel.text = @"创建群组";
        addGroupLabel.backgroundColor = [UIColor clearColor];
        addGroupLabel.font = [UIFont boldSystemFontOfSize:14.0f];
        [addGroupBtn addSubview:addGroupLabel];
        UIImageView * addGrounpImg = [[UIImageView alloc] initWithFrame:CGRectMake(20,12, 20, 20)];
        addGrounpImg.image = [UIImage imageNamed:@"addgroupbutton.png"];
        [addGroupBtn addSubview:addGrounpImg];
        
    }
       
    int tableHeight = isCreatGroup?(SCREEN_HEIGHT-20):(SCREEN_HEIGHT-64-44);
    //群组列表
    _groupTableview = [[UITableView alloc] initWithFrame:CGRectMake(0, isCreatGroup?(YNEW):TITLE_BAR_HEGIHT+(YNEW), SCREEN_WIDTH, tableHeight) style:UITableViewStylePlain];
    _groupTableview.delegate = self;
    _groupTableview.dataSource = self;
    _groupTableview.backgroundColor = [UIColor colorWithRed:236/255.0f green:235/255.0f blue:243/255.0f alpha:1];
    _groupTableview.bounces = NO;
    [self.view addSubview:_groupTableview];
    //显示未激活人员按钮
    UIView * activateView = [[UIView alloc] initWithFrame:CGRectMake(0,(self.view.frame.size.height)-44, (SCREEN_WIDTH)-(SCREEN_WIDTH)*0.2, 44)];
    [activateView setBackgroundColor:[UIColor colorWithRed:236/255.0f green:235/255.0f blue:243/255.0f alpha:1]];
    [self.view addSubview:activateView];
    
    UILabel * chooseLabel = [[UILabel alloc] initWithFrame:CGRectMake(5,5, 180, 30)];
    chooseLabel.text = @"显示未激活人员";
    chooseLabel.backgroundColor = [UIColor clearColor];
    chooseLabel.font = [UIFont boldSystemFontOfSize:14.0f];
    [activateView addSubview:chooseLabel];
    
    _activateSwitchBtn = [[UISwitch alloc] initWithFrame:CGRectMake((SCREEN_WIDTH)-(SCREEN_WIDTH)*0.2-70, 5, 20, 20)];
    [_activateSwitchBtn setOn:YES];
    [_activateSwitchBtn addTarget:self action:@selector(activateSwitchBtn:) forControlEvents:UIControlEventValueChanged];
    [activateView addSubview:_activateSwitchBtn];
    if ([@"OFF" isEqualToString:[DE getGlobalVar:IS_SHOW_UNACTIVE_USER]]) {
        [_activateSwitchBtn setOn:NO];
    }else
    {
        [_activateSwitchBtn setOn:YES];
        
    }
    orglist =  [ContactDao queryAllOrgWithCount];
    
    selectOrgID = [DE getGlobalVar:LOGIN_USER_ORGID];
    
}
-(void)addGroupBtn:(id)sender
{
    //NSLog(@"创建群组");
    AddressBookLeftViewController *colorsVC = [[AddressBookLeftViewController alloc] init];
    colorsVC.isCreatGroup = YES;
    AddressBookViewController *tabView = [[AddressBookViewController alloc] init];
    tabView.isCreatGroup =YES;
    ICSDrawerController *creator = [[ICSDrawerController alloc] initWithLeftViewController:colorsVC
                                                                     centerViewController:tabView];
    creator.canDrawerMove = YES;
    [kAppDelegate.iCSDrawerController.navigationController pushViewController:creator animated:NO];


}


-(void)activateSwitchBtn:(id)sender
{
    if ([_activateSwitchBtn isOn]) {
        [DE setGlobalVar:IS_SHOW_UNACTIVE_USER value:@"ON"];
    }else
    {
        [DE setGlobalVar:IS_SHOW_UNACTIVE_USER value:@"OFF"];
        
    }
    [groupdelegate onGroupClikc:selectOrgID];
    [self.drawer close];

}

- (BOOL)prefersStatusBarHidden
{
    return NO;
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return orglist.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * CellIdentifier = @"AddressBookLeftTableViewCell";                                                           
    AddressBookLeftTableViewCell * cell = [tableView dequeueReusableHeaderFooterViewWithIdentifier:CellIdentifier];
    
    NSString* curtID =[[orglist objectAtIndex:indexPath.row] mID];
    if (cell == nil) {
        cell = [[[NSBundle mainBundle] loadNibNamed:CellIdentifier owner:self options:nil] objectAtIndex:0];
    }
    
    if ([curtID hasPrefix:@"usergrp"]) {
        cell.typeLabel.text = @"用户";
        
    }else if ([curtID hasPrefix:@"virtual"])
    {
        cell.typeLabel.text = @"官方";
    }else
    {
        cell.typeLabel.backgroundColor = [UIColor orangeColor];
        cell.typeLabel.text = @"系统";
    }
    
    if([selectOrgID isEqualToString:curtID]){
        cell.contentView.backgroundColor = [UIColor colorWithRed:71/255.0f green:146/255.0f blue:234/255.0f alpha:0.9];
    }else{
        cell.contentView.backgroundColor = [UIColor whiteColor];
    }
    cell.nameLabel.text = [NSString stringWithFormat:@"%@（%@）",[[orglist objectAtIndex:indexPath.row] mNAME],[[orglist objectAtIndex:indexPath.row] otherInfo]];
    
    return cell;
}

#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
     selectOrgID =[[orglist objectAtIndex:indexPath.row] mID];
    [groupdelegate onGroupClikc:selectOrgID];
    [self.drawer close];
    [_groupTableview reloadData];
}


-(void)refrshOrgData{
    
    if (!isCreatGroup) {
        
        orglist =  [ContactDao queryAllOrgWithCount];
        selectOrgID = [DE getGlobalVar:LOGIN_USER_ORGID];
        [_groupTableview reloadData];
   }
}


#pragma mark - ICSDrawerControllerPresenting

- (void)drawerControllerWillOpen:(ICSDrawerController *)drawerController
{
    self.view.userInteractionEnabled = NO;
}

- (void)drawerControllerDidOpen:(ICSDrawerController *)drawerController
{
    self.view.userInteractionEnabled = YES;
}

- (void)drawerControllerWillClose:(ICSDrawerController *)drawerController
{
    self.view.userInteractionEnabled = NO;
}

- (void)drawerControllerDidClose:(ICSDrawerController *)drawerController
{
    self.view.userInteractionEnabled = YES;
}

@end
